// pages/accountInfo/accountInfo.js
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bankList: [],
    source: '',
    selectedList: [{
      type: 1,
      value: '银行卡'
    }, {
      type: 2,
      value: '支付宝'
    }]
  },
  look(e) {
    let {
      professional_bank_id,
      bank_type
    } = e.currentTarget.dataset.bank;
    wx.navigateTo({
      url: '/pages/bankEdit/bankEdit?id=' + professional_bank_id + '&type=' + bank_type + '&look=true',
    })

  },
  toDetail(e) {
    let {
      type
    } = this.data.selectedList[e.detail.value] || {};
    console.log(type)
    wx.navigateTo({
      url: '/pages/bankEdit/bankEdit?type='+type,
    })
  },
  setDefault(e) {
    let {
      bank
    } = e.currentTarget.dataset, that = this;
    if (bank.is_default == 1) return
    console.log(bank)
    let data = {
      professional_bank_id: bank.professional_bank_id,
      is_default: bank.is_default == 0 ? '1' : '0'
    }
    wx.showLoading({
      title: '加载中...',
      mask: true,
      icon: "none"
    })
    wx.$api.setDefaultBank(data).then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        wx.$Toast(res.data.msg);
        that.getBankcards();
      } else {
        wx.$Toast(res.data.msg);
      }
    }).catch(ret => {
      wx.$Toast(ret.msg || '服务器繁忙，请稍候重试');
    })
  },
  delete(e) {
    let {
      bank
    } = e.currentTarget.dataset;
    let that = this;
    let num=0;
    for(let i=0;i<this.data.bankList.length;i++){
      if(this.data.bankList[i].bank_type=='2'){
        num++
      }
    }
    if (num==1&&bank.bank_type==1) {
      Dialog.alert({
        title: '标题',
        message: '至少有一张银行卡作为账户',
      }).then(() => {
        // on close
      });
      return;
    };
   
    Dialog.confirm({
      title: '确认删除',
      message:'确定删除该账户？',
      asyncClose: true,
      className: 'btnWidth'
    }).then(() => {
      wx.$api.delBank({
        professional_bank_id: bank.professional_bank_id
      }).then(res => {
        Dialog.close();
        if (parseInt(res.data.code) != 200) {
          return wx.$Toast(res.data.msg);
        }
        that.getBankcards();
      })
    }).catch(() => {
      Dialog.close();
    });
  },
  getBankcards() {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    wx.$api.bankcards().then(res => {
      wx.hideLoading();
      if (res.data.code == 200) {
        this.setData({
          bankList: res.data.data
        })
      }
    }).catch(ret => {
      wx.$Toast(ret.msg || "服务繁忙")
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.setData({
    //   source: options.source
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBankcards();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})